package cn.itcast.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SpringRabbitListener {

    /**
     * 模式1：简单队列：监听simpLe.queue队列的消息
     * @param msg
     */
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg) {
//        System.out.println("spring 消费者接收到消息：【" + msg + "】");
//    }


    /***
     * 模式2：work模式：：监听work.queue队列的消息
     * @param msg
     */
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueueMessage1(String msg) {
//        log.info("消费者 1 接收到消息：【" + msg + "】", msg);
//
//        //模拟机器处理的性能差异
//        try {
//            Thread.sleep(20);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }
//
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueueMessage2(String msg) {
//        log.error("----------消费者 2 接收到消息：【" + msg + "】", msg);
//
//        //模拟机器处理的性能差异
//        try {
//            Thread.sleep(200);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }


    /**
     * 模式3：广播模式：监听fanoutExchange交换机的消息
     */

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanouQueueMessage1(String msg) {
        log.info("fanout.queue 1 接收到消息：【" + msg + "】", msg);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanouQueueMessage2(String msg) {
        log.info("fanout.queue 2 接收到消息：【" + msg + "】", msg);
    }

    /**
     * 模式4：定向模式路由模式：监听directExchange交换机的消息
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "direct.queue1"),
                    exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT),
                    key = {"red","blue"}
            )
    )
    public void listenDirect1(String msg) {
        log.info("direct.queue 1 接收到消息：【" + msg + "】", msg);
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "direct.queue2"),
                    exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT),
                    key = {"red","yellow"}
            )
    )
    public void listenDirect2(String msg) {
        log.error("direct.queue 2接收到消息：【" + msg + "】", msg);
    }

    /**
     * 模式5：通配符模式：监听topicExchange交换机的消息
     */

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue1"),
                    exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),
                    key = {"china.#"}
            )
    )
    public void listenTopic1(String msg) {
        log.info("topic.queue 1 接收到消息：【" + msg + "】", msg);
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue2"),
                    exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),
                    key = {"#.news"}
            )
    )
    public void listenTopic2(String msg) {
        log.error("--------------topic.queue 2接收到消息：【" + msg + "】", msg);
    }



    /**
     * 模式6：消息转换器
     * @param msg
     */
    @RabbitListener(queues = "simple.queue")
      public void listenSimpleQueueMessage(Object msg) {
       System.out.println("spring 消费者接收到消息：【" + msg + "】");
    }




}